<?php

$username = $_POST['username'];
$password = $_POST['password'];

include("opendb.php");
include("functions.php");
$res = isValidUser($username,$password);
if($res!=1) {
echo "$username = $password";
exit;
}

$type = $_POST['type'];

//Add_Image($ListingID, "../listing_images/",$images, 0, $_SESSION["id"]);
$uploadpath = "../user_images/"; 
    $source = $HTTP_POST_FILES['image_file']['tmp_name']; 
    $dest = ''; 
	$filename='';
    if ( ($source != 'none') && ($source != '' )) { 

        $imagesize = getimagesize($source); 
		$imagetype = $imagesize[2];
        switch ( $imagesize[2] ) { 

            case 0: 

//                echo '<BR> Image is unknown <BR>'; 
                break; 

            case 1: 
//                echo '<BR> Image is a GIF <BR>'; 
                $dest = uniqid('img').'.gif'; 
                break; 
             
            case 2: 
//                echo '<BR> Image is a JPG <BR>'; 
                $dest = uniqid('img').'.jpg'; 
                break; 
             
            case 3: 
 //               echo '<BR> Image is a PNG <BR>'; 
                //$dest = uniqid('img').'.png'; 
                break; 
        } 

        if ( $dest != '' ) { 

            if ( move_uploaded_file( $source, $uploadpath.$dest ) ) { 
				chmod($uploadpath.$dest, 0644);
				
				if($type=='image') {
					//CreateThumbnail($source,$outputfile,$size)
					CreateThumbnailAgents($uploadpath.$dest,$uploadpath."t_".$dest,100);
					//CreateResizeWidth($uploadpath.$dest,$uploadpath."resize_".$dest,400);
					
					 $query = "update Users set Image = '".$dest."' where Username = '".$username."'";
					
					//echo $query;
					mysql_query($query) or die(mysql_error());
					$message = "2"; 
                } else if($type=="logo")
				{
						if($imagesize[0] > 100)
						  CreateThumbnailAgents($uploadpath.$dest,$uploadpath."t_".$dest,100);
						else
						  CreateThumbnailAgents($uploadpath.$dest,$uploadpath."t_".$dest,$imagesize[0]);
						
						//CreateResizeWidth($uploadpath.$dest,$uploadpath."resize_".$dest,400);
						$query = "update Users set Company_Logo = '".$dest."' where Username = '".$username."'";
						//echo $query;
						mysql_query($query) or die(mysql_error()); 
						$message = "2";
				}
            } else { 
				$message='-1'; 
				
            } 

        }  

   		 } 
		 else 
		 { 

	        $message='-2'; 
			
    	}
echo $message;


function CreateThumbnailAgents($inputfile,$outputfile,$size)
{
	//size tell how wide the thumbnail will generate, then we format it to 100x75
	//$size = 100;
	$width = $size;
	$filename = $inputfile;
	$width2 = 100;
	$height2 = 75;
	// Get dimensions of existing image
	list($width_orig, $height_orig, $image_type) = getimagesize($filename);
	
	//reduce the image to generate a good thumbnail
	$ratio = $size/$width_orig;
	$height = $height_orig*$ratio;
	//echo $height."<BR>";
	
	// Prepare canvas
	$canvas = imagecreatetruecolor($width,$height);
	
	$str =ereg_replace(".*\.(.*)$","\\1",$filename);
	$str =strtoupper($str);
	if ($str=="JPG" || $str=="JPEG")
	{
	$image = imagecreatefromjpeg($filename);
	}
	else
	{
	//echo "hti";
	$image = imagecreatefromgif($filename);
	}
	
	imagecopyresampled($canvas, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

	// Output
	 switch($image_type)
		  {
			case 2:
			{
				imagejpeg($canvas,$outputfile,100);
				break;
			}
			case 1:
			{
			
				imagegif($canvas,$outputfile,100);
				break;
			}
		  }
}

?>